/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package net.sf.odinms.net.channel.handler;

import net.sf.odinms.client.IEquip;
import net.sf.odinms.client.IItem;
import net.sf.odinms.client.MapleClient;
import net.sf.odinms.client.MapleInventory;
import net.sf.odinms.client.MapleInventoryType;
import net.sf.odinms.net.AbstractMaplePacketHandler;
import net.sf.odinms.server.MapleItemInformationProvider;
import net.sf.odinms.tools.MaplePacketCreator;
import net.sf.odinms.tools.data.input.SeekableLittleEndianAccessor;

/**
 *
 * @author Small丶Hyun
 */
public final class 放大镜 extends AbstractMaplePacketHandler {
    //放大镜
    	private static int JD_ValueListB[] = {
		1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
        11, 12, 13, 14, 901, 902, 903, 904, 905, 10001,
        10002, 10003, 10004, 10005, 10006, 10007, 10008, 10009, 10010, 10011,
        10012, 10013, 10014, 10041, 10042, 10043, 10044, 10045, 10046, 10047,
        10048, 10051, 10052, 10053, 10054, 10055, 10070, 10081, 10151, 10156,
        10201, 10206, 10221, 10226, 10231, 10236, 10241, 10246, 10291, 10070,
        10081, 10151, 10156, 10201, 10206, 10221, 10226, 10231, 10236, 10241,
        10246, 10291, 10051, 10052, 10053, 10054, 10055, 10041, 10042, 10043,
        10044, 10045, 10046, 10047, 10048, 10001, 10002, 10003, 10004, 10005,
        10006, 10007, 10008, 10009, 10010, 10011, 10012, 10013, 10014
	};
	private static int JD_ValueListA[] = {
		20051, 20052, 20053, 20054, 20055, 20070, 20086, 20201, 20206, 20291,
        20351, 20352, 20353, 20366, 20396, 20401, 20406, 20041, 20042, 20043,
        20044, 20045, 20046, 20047, 20048, 20051, 20052, 20053, 20054, 20055,
        20070, 20086, 20201, 20206, 20291, 20351, 20352, 20353, 20366, 20396,
        20401, 20406, 20041, 20042, 20043, 20044, 20045, 20046, 20047, 20048,
        20051, 20052, 20053, 20054, 20055, 20070, 20086, 20201, 20206, 20291,
        20351, 20352, 20353, 20366, 20396, 20401, 20406, 20041, 20042, 20043,
        20044, 20045, 20046, 20047, 20048, 20051, 20052, 20053, 20054, 20055,
        20070, 20086, 20201, 20206, 20291, 20351, 20352, 20353, 20366, 20396,
        20401, 20406, 20041, 20042, 20043, 20044, 20045, 20046, 20047, 20048,
	};
	private static int JD_ValueListS[] = {
		30041, 30042, 30043, 30044, 30045, 30046, 30047, 30048, 30051, 30052,
        30053, 30054, 30055, 30086, 30106, 30107, 30291, 30356, 30357, 30366,
        30371, 30376, 30551, 30601, 30602, 30701, 30702, 31001, 31002, 31003,
        31004, 30041, 30042, 30043, 30044, 30045, 30046, 30047, 30048, 30051,
        30052, 30053, 30054, 30055, 30070, 30086, 30106, 30107, 30291, 30356,
        30357, 30366, 30371, 30376, 30551, 30601, 30602, 30701, 30702, 31001,
        31002, 31003, 31004
	};
    	public static final int getJd_levelForid(int id)
	{
		for (int i = 0; i < JD_ValueListB.length; i++)
			if (JD_ValueListB[i] == id)
				return 5;

		for (int i = 0; i < JD_ValueListA.length; i++)
			if (JD_ValueListA[i] == id)
				return 6;

		for (int i = 0; i < JD_ValueListS.length; i++)
			if (JD_ValueListS[i] == id)
				return 7;

		return 0;
	}
    public void handlePacket(SeekableLittleEndianAccessor slea, MapleClient c) {
        System.out.println("放大镜："+slea.toString());
		slea.skip(4);
		byte slot = (byte)slea.readShort();
		byte dst = (byte)slea.readShort();
		MapleItemInformationProvider ii = MapleItemInformationProvider.getInstance();
		IEquip toScroll;
		if (dst < 0)
			toScroll = (IEquip)c.getPlayer().getInventory(MapleInventoryType.EQUIPPED).getItem(dst);
		else
			toScroll = (IEquip)c.getPlayer().getInventory(MapleInventoryType.EQUIP).getItem(dst);
		if (toScroll == null)
			return;
		byte oldLevel = toScroll.getIdentify();
		if (oldLevel == 1)
		{
			MapleInventory useInventory = c.getPlayer().getInventory(MapleInventoryType.USE);
			IItem scroll = useInventory.getItem(slot);
			if (scroll.getQuantity() <= 0)
				return;
			switch (scroll.getItemId())
			{
			case 2460000:
			case 2460001:
			case 2460002:
			case 2460003:
				int ch1 = 0;
				int ch2 = 0;
				int ch3 = 0;
				int ch1type = 0;
				int ch2type = 0;
				int ch3type = 0;
				int sh = 0;
				int Sj = 3;
				int Bj = 20;
				if (toScroll.getPotential_1() != 0)
					sh++;
				if (toScroll.getPotential_2() != 0)
					sh++;
				if (toScroll.getPotential_3() != 0)
					sh++;
				int chec = 0;
				for (int i = 0; i < sh; i++)
				{
					if (i == 0)
					{
						if (getJd_levelForid(toScroll.getPotential_1()) == 7)
							chec = (int)(Math.random() * (double)Sj);
						else
						if (getJd_levelForid(toScroll.getPotential_1()) == 6)
							chec = (int)(Math.random() * (double)Bj);
						else
							chec = (int)(Math.random() * 200D);
						if (chec < Sj)
						{
							ch1type = 7;
							ch1 = (int)(Math.random() * (double)JD_ValueListS.length);
							toScroll.setPotential_1((short)JD_ValueListS[ch1]);
							continue;
						}
						if (chec < Bj)
						{
							ch1type = 6;
							ch1 = (int)(Math.random() * (double)JD_ValueListA.length);
							toScroll.setPotential_1((short)JD_ValueListA[ch1]);
						} else
						{
							ch1type = 5;
							ch1 = (int)(Math.random() * (double)JD_ValueListB.length);
							toScroll.setPotential_1((short)JD_ValueListB[ch1]);
						}
						continue;
					}
					if (i == 1)
					{
						if (getJd_levelForid(toScroll.getPotential_2()) == 7)
							chec = (int)(Math.random() * (double)Sj);
						else
						if (getJd_levelForid(toScroll.getPotential_2()) == 6)
							chec = (int)(Math.random() * (double)Bj);
						else
						if (ch1type == 7)
							chec = (int)(Math.random() * (double)Sj);
						else
						if (ch1type == 6)
							chec = (int)(Math.random() * 150D);
						else
							chec = (int)(10D + Math.random() * 200D);
						if (chec < Sj)
						{
							ch2type = 7;
							ch2 = (int)(Math.random() * (double)JD_ValueListS.length);
							toScroll.setPotential_2((short)JD_ValueListS[ch2]);
							continue;
						}
						if (chec < Bj)
						{
							ch2type = 6;
							ch2 = (int)(Math.random() * (double)JD_ValueListA.length);
							toScroll.setPotential_2((short)JD_ValueListA[ch2]);
						} else
						{
							ch2type = 5;
							ch2 = (int)(Math.random() * (double)JD_ValueListB.length);
							toScroll.setPotential_2((short)JD_ValueListB[ch2]);
						}
						continue;
					}
					if (i == 2)
					{
						if (getJd_levelForid(toScroll.getPotential_3()) == 7)
							chec = (int)(Math.random() * (double)Sj);
						else
						if (getJd_levelForid(toScroll.getPotential_3()) == 6)
							chec = (int)(Math.random() * (double)Bj);
						else
						if (ch2type == 7)
							chec = (int)(Math.random() * (double)Sj);
						else
						if (ch2type == 6)
							chec = (int)(Math.random() * 150D);
						else
							chec = (int)(10D + Math.random() * 200D);
						if (chec < Sj)
						{
							ch3type = 7;
							ch3 = (int)(Math.random() * (double)JD_ValueListS.length);
							toScroll.setPotential_3((short)JD_ValueListS[ch3]);
							continue;
						}
						if (chec < Bj)
						{
							ch3type = 6;
							ch3 = (int)(Math.random() * (double)JD_ValueListA.length);
							toScroll.setPotential_3((short)JD_ValueListA[ch3]);
						} else
						{
							ch3type = 5;
							ch3 = (int)(Math.random() * (double)JD_ValueListB.length);
							toScroll.setPotential_3((short)JD_ValueListB[ch3]);
						}
					} else
					{
						c.getPlayer().弹窗("错误");
					}
				}

				int level = 0;
				for (int i = 0; i < 3; i++)
				{
					if (i == 0)
					{
						level = ch1type;
						continue;
					}
					if (i == 1)
					{
						if (ch2type > level)
							level = ch2type;
						continue;
					}
					if (ch3type > level)
						level = ch3type;
				}

				toScroll.setIdentify((byte)level);
				toScroll.setIdentified((byte) (toScroll.getIdentified() + 1));
				break;

			default:
				return;
			}
			useInventory.removeItem(scroll.getPosition(), (short)1, false);
			c.getSession().write(MaplePacketCreator.scrolledItem(scroll, toScroll, false));
			c.getPlayer().getMap().broadcastMessage(MaplePacketCreator.showMagniflerEffect(c.getPlayer().getId(), dst));
			c.getPlayer().equipChanged();
			c.getPlayer().saveToDB(true);
		} else{
            c.getPlayer().弹窗("无法使用放大镜的道具。");
        }
		c.getSession().write(MaplePacketCreator.enableActions());
	}
}